package pers.mingshan.thread.thread;

/**
 * 线程同步
 * @author mingshan
 *
 */
public class ThreadSynchronizedDemo {

    public static void main(String[] args) {
      new ThreadSynchronizedDemo().init();
    }

    public void init() {
       final outPuter o=new outPuter();
       new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        o.outPut("hanjuntao");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        o.outPut("mahuateng");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    static class outPuter {
        public  void outPut(String name) {
            int len = name.length();
            synchronized(this) {
                for (int i = 0; i < len; i++) {
                    System.out.print(name.charAt(i)); 
                }
                System.out.println();
            }
        }
    }
}
